package com.liwenfeng.diancanspringboot.service.impl;

import com.liwenfeng.diancanspringboot.common.context.BaseContext;
import com.liwenfeng.diancanspringboot.common.exception.OrderBusinessException;
import com.liwenfeng.diancanspringboot.common.exception.UserNotLoginException;
import com.liwenfeng.diancanspringboot.mapper.OrderDetailMapper;
import com.liwenfeng.diancanspringboot.mapper.OrderMapper;
import com.liwenfeng.diancanspringboot.pojo.entity.OrderDetail;
import com.liwenfeng.diancanspringboot.pojo.entity.Orders;
import com.liwenfeng.diancanspringboot.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;

    // 更新订单详情状态（手机端admin模块)(浏览器端）
    @Transactional
    @Override
    public void update(OrderDetail orderDetail) {
        OrderDetail orderDetail1 = orderDetailMapper.getById(orderDetail.getId());
        if (orderDetail.getOrderId()==null){
            if (orderDetail1.getStatus() != orderDetail.getStatus() - 1) {
                throw new OrderBusinessException("状态错误，请刷新后重新操作");
            }
            if (orderDetail.getStatus() == 3) {
                orderDetail.setWaiterId(BaseContext.getCurrentId());
            } else if (orderDetail.getStatus() == 1 || orderDetail.getStatus() == 2) {
                orderDetail.setChefId(BaseContext.getCurrentId());
            }
        }else {
            if (Objects.equals(orderDetail1.getStatus(), orderDetail.getStatus()) || !Objects.equals(orderDetail.getOrderId(), orderDetail1.getOrderId()) || orderDetail1.getStatus() == 4) {
                throw new OrderBusinessException("状态错误，请刷新后重新操作");
            }
        }
        orderDetailMapper.update(orderDetail);

        if (orderDetail.getStatus() == 3 || orderDetail.getStatus() == 4) {
            List<OrderDetail> orderDetailList = orderDetailMapper.getValidByOrderId(orderDetail1.getOrderId());
            Orders orders = new Orders();
            orders.setId(orderDetail1.getOrderId());
            if (orderDetailList == null || orderDetailList.size() == 0) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("用户取消");
                orders.setAmount(BigDecimal.valueOf(0));
            }else {
                double sum = orderDetailList.stream().mapToDouble(item -> item.getAmount().doubleValue() * item.getNumber()).sum();
                List<OrderDetail> toList = orderDetailList.stream().filter(item -> item.getStatus() != 3).toList();
                orders.setAmount(BigDecimal.valueOf(sum));
                if (toList.size() == 0) {
                    // 更新订单状态,状态转为完成
                    orders.setStatus(Orders.COMPLETED);
                    orders.setCompleteTime(LocalDateTime.now());
                }
            }
            orderMapper.update(orders);
        }
    }

    // 按状态获取订单详情列表（手机端admin模块)
    @Override
    public List<OrderDetail> getOrderDetailByStatus() {
        String job = BaseContext.getOther();
        if (job == null)
            throw new UserNotLoginException("参数错误，请重新登录");
        List<Integer> orderDetailStatusList = new ArrayList<>();
        if (job.equals("厨师")) {
            orderDetailStatusList.add(OrderDetail.TO_BE_DOING);
            orderDetailStatusList.add(OrderDetail.DOING);
        } else if (job.equals("服务员")) {
            orderDetailStatusList.add(OrderDetail.TO_BE_SEND);
        } else if (List.of("0", "1","管理员").contains(job)) {
            orderDetailStatusList.add(OrderDetail.TO_BE_DOING);
            orderDetailStatusList.add(OrderDetail.DOING);
            orderDetailStatusList.add(OrderDetail.TO_BE_SEND);
        }
        if (orderDetailStatusList.size() == 0)
            return null;
        List<OrderDetail> orderDetails = orderDetailMapper.listByStatus(orderDetailStatusList);
        return orderDetails;
    }
}
